<?php
// 邮件发送配置
function wp_smtp($phpmailer){
    $phpmailer->Mailer = "smtp";
    $phpmailer->From = 'service@fangfajr.com';
    $phpmailer->FromName = '方法金融';
    $phpmailer->Sender = $phpmailer->From; //Return-Path
    $phpmailer->AddReplyTo($phpmailer->From,$phpmailer->FromName); //Reply-To
    $phpmailer->Host = 'smtp.exmail.qq.com';
    $phpmailer->SMTPSecure = 'ssl';
    $phpmailer->Port = '465';
    $phpmailer->SMTPAuth = TRUE;
    $phpmailer->Username = 'service@fangfajr.com';
    $phpmailer->Password = 'why4myQQ';
}
add_action('phpmailer_init','wp_smtp');

// Custom WordPress Admin CSS
function admin_css() {
    echo '<style>body {font-family:Verdana, Geneva, "Microsoft Yahei";}
        #wpadminbar * { font-family:Georgia, "Microsoft Yahei";}
        .welcome-panel h3, h2 .nav-tab, .wrap h2, .subtitle, .login form .input,
        .media-modal, .media-frame { font-family:"HelveticaNeue-Light","Helvetica Neue Light","Helvetica Neue","Microsoft Yahei";}

        #edit-slug-box { min-height: 0;}
        textarea.code_area { width:90%;height:80px;font-size:12px;line-height:1.5;font-family:Consolas, Monaco, "Courier New", Courier, monospace;}
        input[type="checkbox"].regular-text { width: auto;}

        #activity-widget #the-comment-list img { width: 50px; height: 50px;}
    </style>';
}
add_action('admin_head', 'admin_css');

// REMOVE WP EMOJI
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');

// 禁用 REST API
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');

// 移除 wp-json 链接
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );

function get_ssl_avatar($avatar) {
   $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&.*/','<img src="https://secure.gravatar.com/avatar/$1?s=$2" class="avatar avatar-$2" height="$2" width="$2">',$avatar);
   return $avatar;
}
add_filter('get_avatar', 'get_ssl_avatar');

// Remove Open Sans that WP adds from frontend
if (!function_exists('remove_wp_open_sans')) :
    function remove_wp_open_sans() {
        wp_deregister_style( 'open-sans' );
        wp_register_style( 'open-sans', false );
    }

    // 前台删除Google字体CSS
    // add_action('wp_enqueue_scripts', 'remove_wp_open_sans');
    // 后台删除Google字体CSS
    add_action('admin_enqueue_scripts', 'remove_wp_open_sans');
endif;

// 修改 jQuery 地址
function add_scripts() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', '//cdn.bootcss.com/jquery/1.12.3/jquery.min.js', array(), '');
    wp_enqueue_script( 'jquery' );
}
if ( ! is_admin() ) :
    add_action('wp_enqueue_scripts', 'add_scripts');
endif;

// 重命名上传的中文文件名
function kfx_upload_prefilter($file){
    if( preg_match("/[\x7f-\xff]/", $file['name']) ){
        date_default_timezone_set('Asia/Shanghai');
        $time = date('ymdHis', time());
        $file['name'] = $time .''. mt_rand(1,100) .'.'. pathinfo($file['name'] , PATHINFO_EXTENSION);
    }
    return $file;
}
add_filter('wp_handle_upload_prefilter', 'kfx_upload_prefilter');

/* 给 编辑 增加模板相关配置权限 */
// function add_theme_caps() {
//     $editor = get_role( 'editor' );
//     $editor->add_cap( 'edit_theme_options' );
// }
// add_action( 'admin_init', 'add_theme_caps');

// 启用友情链接
add_filter( 'pre_option_link_manager_enabled', '__return_true' );

add_filter( 'get_the_archive_title', function ( $title ) {
    if( is_category() ) {
        $title = single_cat_title( '', false );
    }
    return $title;
});

// 使小工具标题支持 HTML
add_filter( 'widget_title', 'html_widget_title_replace' );

function html_widget_title_replace($html_widget_title) {
    $html_widget_title_tagopen = '['; //Our HTML opening tag replacement
    $html_widget_title_tagclose = ']'; //Our HTML closing tag replacement

    $html_widget_title = str_replace($html_widget_title_tagopen, '<', $html_widget_title);
    $html_widget_title = str_replace($html_widget_title_tagclose, '>', $html_widget_title);

    return $html_widget_title;
}

?>
